/*
 *  Copyright 1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

//package org.apache.tomcat.util.http;
package com.ropzsu.scalaWeb.util;

import java.net.*;
import java.util.*;


/**
 * A mime type map that implements the java.net.FileNameMap interface.
 *
 * @author James Duncan Davidson [duncan@eng.sun.com]
 * @author Jason Hunter [jch@eng.sun.com]
 */
public class MimeMap implements FileNameMap {

    // Defaults - all of them are "well-known" types,
    // you can add using normal web.xml.
    
    public static Hashtable<String, String> defaultMap=new Hashtable<String, String>(101);
    static {
          defaultMap.put("323", "text/h323"); 
          defaultMap.put("acx", "application/internet-property-stream"); 
          defaultMap.put("ai", "application/postscript"); 
          defaultMap.put("aif", "audio/x-aiff"); 
          defaultMap.put("aifc", "audio/x-aiff"); 
          defaultMap.put("aiff", "audio/x-aiff"); 
          defaultMap.put("asf", "video/x-ms-asf"); 
          defaultMap.put("asr", "video/x-ms-asf"); 
          defaultMap.put("asx", "video/x-ms-asf"); 
          defaultMap.put("au", "audio/basic"); 
          defaultMap.put("avi", "video/x-msvideo"); 
          defaultMap.put("axs", "application/olescript"); 
          defaultMap.put("bas", "text/plain"); 
          defaultMap.put("bcpio", "application/x-bcpio"); 
          defaultMap.put("bin", "application/octet-stream"); 
          defaultMap.put("bmp", "image/bmp"); 
          defaultMap.put("c", "text/plain"); 
          defaultMap.put("cat", "application/vnd.ms-pkiseccat"); 
          defaultMap.put("cdf", "application/x-cdf"); 
          defaultMap.put("cer", "application/x-x509-ca-cert"); 
          defaultMap.put("class", "application/octet-stream"); 
          defaultMap.put("clp", "application/x-msclip"); 
          defaultMap.put("cmx", "image/x-cmx"); 
          defaultMap.put("cod", "image/cis-cod"); 
          defaultMap.put("cpio", "application/x-cpio"); 
          defaultMap.put("crd", "application/x-mscardfile"); 
          defaultMap.put("crl", "application/pkix-crl"); 
          defaultMap.put("crt", "application/x-x509-ca-cert"); 
          defaultMap.put("csh", "application/x-csh"); 
          defaultMap.put("css", "text/css"); 
          defaultMap.put("dcr", "application/x-director"); 
          defaultMap.put("der", "application/x-x509-ca-cert"); 
          defaultMap.put("dir", "application/x-director"); 
          defaultMap.put("dll", "application/x-msdownload"); 
          defaultMap.put("dms", "application/octet-stream"); 
          defaultMap.put("doc", "application/msword"); 
          defaultMap.put("dot", "application/msword"); 
          defaultMap.put("dvi", "application/x-dvi"); 
          defaultMap.put("dxr", "application/x-director"); 
          defaultMap.put("eps", "application/postscript"); 
          defaultMap.put("etx", "text/x-setext"); 
          defaultMap.put("evy", "application/envoy"); 
          defaultMap.put("exe", "application/octet-stream"); 
          defaultMap.put("fif", "application/fractals"); 
          defaultMap.put("flr", "x-world/x-vrml"); 
          defaultMap.put("gif", "image/gif"); 
          defaultMap.put("gtar", "application/x-gtar"); 
          defaultMap.put("gz", "application/x-gzip"); 
          defaultMap.put("h", "text/plain"); 
          defaultMap.put("hdf", "application/x-hdf"); 
          defaultMap.put("hlp", "application/winhlp"); 
          defaultMap.put("hqx", "application/mac-binhex40"); 
          defaultMap.put("hta", "application/hta"); 
          defaultMap.put("htc", "text/x-component"); 
          defaultMap.put("htm", "text/html"); 
          defaultMap.put("html", "text/html"); 
          defaultMap.put("htt", "text/webviewhtml"); 
          defaultMap.put("ico", "image/x-icon"); 
          defaultMap.put("ief", "image/ief"); 
          defaultMap.put("iii", "application/x-iphone"); 
          defaultMap.put("ins", "application/x-internet-signup"); 
          defaultMap.put("isp", "application/x-internet-signup"); 
          defaultMap.put("jfif", "image/pipeg"); 
          defaultMap.put("jpe", "image/jpeg"); 
          defaultMap.put("jpeg", "image/jpeg"); 
          defaultMap.put("jpg", "image/jpeg"); 
          defaultMap.put("js", "application/x-javascript"); 
          defaultMap.put("latex", "application/x-latex"); 
          defaultMap.put("lha", "application/octet-stream"); 
          defaultMap.put("lsf", "video/x-la-asf"); 
          defaultMap.put("lsx", "video/x-la-asf"); 
          defaultMap.put("lzh", "application/octet-stream"); 
          defaultMap.put("m13", "application/x-msmediaview"); 
          defaultMap.put("m14", "application/x-msmediaview"); 
          defaultMap.put("m3u", "audio/x-mpegurl"); 
          defaultMap.put("man", "application/x-troff-man"); 
          defaultMap.put("mdb", "application/x-msaccess"); 
          defaultMap.put("me", "application/x-troff-me"); 
          defaultMap.put("mht", "message/rfc822"); 
          defaultMap.put("mhtml", "message/rfc822"); 
          defaultMap.put("mid", "audio/mid"); 
          defaultMap.put("mny", "application/x-msmoney"); 
          defaultMap.put("mov", "video/quicktime"); 
          defaultMap.put("movie", "video/x-sgi-movie"); 
          defaultMap.put("mp2", "video/mpeg"); 
          defaultMap.put("mp3", "audio/mpeg"); 
          defaultMap.put("mpa", "video/mpeg"); 
          defaultMap.put("mpe", "video/mpeg"); 
          defaultMap.put("mpeg", "video/mpeg"); 
          defaultMap.put("mpg", "video/mpeg"); 
          defaultMap.put("mpp", "application/vnd.ms-project"); 
          defaultMap.put("mpv2", "video/mpeg"); 
          defaultMap.put("ms", "application/x-troff-ms"); 
          defaultMap.put("mvb", "application/x-msmediaview"); 
          defaultMap.put("nws", "message/rfc822"); 
          defaultMap.put("oda", "application/oda"); 
          defaultMap.put("p10", "application/pkcs10"); 
          defaultMap.put("p12", "application/x-pkcs12"); 
          defaultMap.put("p7b", "application/x-pkcs7-certificates"); 
          defaultMap.put("p7c", "application/x-pkcs7-mime"); 
          defaultMap.put("p7m", "application/x-pkcs7-mime"); 
          defaultMap.put("p7r", "application/x-pkcs7-certreqresp"); 
          defaultMap.put("p7s", "application/x-pkcs7-signature"); 
          defaultMap.put("pbm", "image/x-portable-bitmap"); 
          defaultMap.put("pdf", "application/pdf"); 
          defaultMap.put("pfx", "application/x-pkcs12"); 
          defaultMap.put("pgm", "image/x-portable-graymap"); 
          defaultMap.put("pko", "application/ynd.ms-pkipko"); 
          defaultMap.put("pma", "application/x-perfmon"); 
          defaultMap.put("pmc", "application/x-perfmon"); 
          defaultMap.put("pml", "application/x-perfmon"); 
          defaultMap.put("pmr", "application/x-perfmon"); 
          defaultMap.put("pmw", "application/x-perfmon"); 
          defaultMap.put("pnm", "image/x-portable-anymap"); 
          defaultMap.put("pot,", "application/vnd.ms-powerpoint"); 
          defaultMap.put("ppm", "image/x-portable-pixmap"); 
          defaultMap.put("pps", "application/vnd.ms-powerpoint"); 
          defaultMap.put("ppt", "application/vnd.ms-powerpoint"); 
          defaultMap.put("prf", "application/pics-rules"); 
          defaultMap.put("ps", "application/postscript"); 
          defaultMap.put("pub", "application/x-mspublisher"); 
          defaultMap.put("qt", "video/quicktime"); 
          defaultMap.put("ra", "audio/x-pn-realaudio"); 
          defaultMap.put("ram", "audio/x-pn-realaudio"); 
          defaultMap.put("ras", "image/x-cmu-raster"); 
          defaultMap.put("rgb", "image/x-rgb"); 
          defaultMap.put("rmi", "audio/mid"); 
          defaultMap.put("roff", "application/x-troff"); 
          defaultMap.put("rtf", "application/rtf"); 
          defaultMap.put("rtx", "text/richtext"); 
          defaultMap.put("scd", "application/x-msschedule"); 
          defaultMap.put("sct", "text/scriptlet"); 
          defaultMap.put("setpay", "application/set-payment-initiation"); 
          defaultMap.put("setreg", "application/set-registration-initiation"); 
          defaultMap.put("sh", "application/x-sh"); 
          defaultMap.put("shar", "application/x-shar"); 
          defaultMap.put("sit", "application/x-stuffit"); 
          defaultMap.put("snd", "audio/basic"); 
          defaultMap.put("spc", "application/x-pkcs7-certificates"); 
          defaultMap.put("spl", "application/futuresplash"); 
          defaultMap.put("src", "application/x-wais-source"); 
          defaultMap.put("sst", "application/vnd.ms-pkicertstore"); 
          defaultMap.put("stl", "application/vnd.ms-pkistl"); 
          defaultMap.put("stm", "text/html"); 
          defaultMap.put("svg", "image/svg+xml"); 
          defaultMap.put("sv4cpio", "application/x-sv4cpio"); 
          defaultMap.put("sv4crc", "application/x-sv4crc"); 
          defaultMap.put("swf", "application/x-shockwave-flash"); 
          defaultMap.put("t", "application/x-troff"); 
          defaultMap.put("tar", "application/x-tar"); 
          defaultMap.put("tcl", "application/x-tcl"); 
          defaultMap.put("tex", "application/x-tex"); 
          defaultMap.put("texi", "application/x-texinfo"); 
          defaultMap.put("texinfo", "application/x-texinfo"); 
          defaultMap.put("tgz", "application/x-compressed"); 
          defaultMap.put("tif", "image/tiff"); 
          defaultMap.put("tiff", "image/tiff"); 
          defaultMap.put("tr", "application/x-troff"); 
          defaultMap.put("trm", "application/x-msterminal"); 
          defaultMap.put("tsv", "text/tab-separated-values"); 
          defaultMap.put("txt", "text/plain"); 
          defaultMap.put("uls", "text/iuls"); 
          defaultMap.put("ustar", "application/x-ustar"); 
          defaultMap.put("vcf", "text/x-vcard"); 
          defaultMap.put("vrml", "x-world/x-vrml"); 
          defaultMap.put("wav", "audio/x-wav"); 
          defaultMap.put("wcm", "application/vnd.ms-works"); 
          defaultMap.put("wdb", "application/vnd.ms-works"); 
          defaultMap.put("wks", "application/vnd.ms-works"); 
          defaultMap.put("wmf", "application/x-msmetafile"); 
          defaultMap.put("wps", "application/vnd.ms-works"); 
          defaultMap.put("wri", "application/x-mswrite"); 
          defaultMap.put("wrl", "x-world/x-vrml"); 
          defaultMap.put("wrz", "x-world/x-vrml"); 
          defaultMap.put("xaf", "x-world/x-vrml"); 
          defaultMap.put("xbm", "image/x-xbitmap"); 
          defaultMap.put("xml", "text/xml");
          defaultMap.put("xsl", "text/xml");
          defaultMap.put("xla", "application/vnd.ms-excel"); 
          defaultMap.put("xlc", "application/vnd.ms-excel"); 
          defaultMap.put("xlm", "application/vnd.ms-excel"); 
          defaultMap.put("xls", "application/vnd.ms-excel"); 
          defaultMap.put("xlt", "application/vnd.ms-excel"); 
          defaultMap.put("xlw", "application/vnd.ms-excel"); 
          defaultMap.put("xof", "x-world/x-vrml"); 
          defaultMap.put("xpm", "image/x-xpixmap"); 
          defaultMap.put("xwd", "image/x-xwindowdump"); 
          defaultMap.put("z", "application/x-compress"); 
          defaultMap.put("zip", "application/zip"); 
    }
    

    private Hashtable<String, String> map = new Hashtable<String, String>();

    public void addContentType(String extn, String type) {
        map.put(extn, type.toLowerCase());
    }

    public Enumeration getExtensions() {
        return map.keys();
    }

    public String getContentType(String extn) {
        String type = (String)map.get(extn.toLowerCase());
	if( type == null ) type=(String)defaultMap.get( extn );
	return type;
    }

    public void removeContentType(String extn) {
        map.remove(extn.toLowerCase());
    }

    /** Get extension of file, without fragment id
     */
    public static String getExtension( String fileName ) {
        // play it safe and get rid of any fragment id
        // that might be there
	int length=fileName.length();
	
        int newEnd = fileName.lastIndexOf('#');
	if( newEnd== -1 ) newEnd=length;
	// Instead of creating a new string.
	//         if (i != -1) {
	//             fileName = fileName.substring(0, i);
	//         }
        int i = fileName.lastIndexOf('.', newEnd );
        if (i != -1) {
             return  fileName.substring(i + 1, newEnd );
        } else {
            // no extension, no content type
            return null;
        }
    }
    
    public String getContentTypeFor(String fileName) {
	String extn=getExtension( fileName );
        if (extn!=null) {
            return getContentType(extn);
        } else {
            // no extension, no content type
            return null;
        }
    }

}

